<?PHP


class music
{
	var $post;
	var $where;
	var $data;
	var $input;
	var $output;
	var $files;
	var $mode;
	var $playlist;
	var $playlist_name;
	var $options;
	
	function log_media()
	{
		
		$this->post['date']=date("Y-m-d H:i:s",time());
		$this->post['content']=$_POST['content'];
		$this->post['action']=$_POST['action'];
		$this->post['media']=$_POST['media_id'];
	
		process_sql_post($this->post,'media_log','add',NULL,NULL);
		
		$w[0]='id';
		$w[1]=$_POST['media_id'];
		$w[2]='=';
		$where[]=$w;
		$td=get_sql('media','id',$where,$o);
		$data=current($td);
		$post[$_POST['action']]=$data[$_POST['action']]+1;
		process_sql_post($post,'media','update','id',$data['id']);

	}
	
	function request_media()
	{
		

		
		$w[0]='id';
		$w[1]=$_POST['id'];
		$w[2]='=';
		$where[]=$w;
		$td=get_sql('media','id',$where,$o);
		$data=current($td);
		$post['request']=$data['request']+1;
		process_sql_post($post,'media','update','id',$data['id']);

	}
	function process_for_adding()
	{
		foreach($this->data as $x)
		{
			$var=$x['id'].'/'.$this->playlist;
		//	$z[]=$x;
			$y[]=$z;
		}	
		
		$this->data=$y;
	}
	function get_music()
	{
			//
			if(!empty($this->options['letter']))
			{
				$w[0]='artist';
				$w[1]=$this->options['letter'].'%';
				$w[2]='LIKE';
				$where[]=$w;
			}
			else
			{
				$where=process_input($this->where);
			}
			$w[0]='type';
			$w[1]='song';
			$w[2]='=';
			$where[]=$w;
		
			if(empty($this->options))
			{
				$o['order'][0]='artist';
				$o['order'][1]='ASC';
			}
			else
			{
				$o=$this->options;
			}
			
			$this->data=get_sql('media','id',$where,$o);
	}
	

	
	
	
	function get_playlist()
	{
			global $conf;
			$where=process_input($this->where);
			$this->data=get_sql('playlist','id',$where,NULL);
	}
	function get_playlist_queue($playlist)
	{
	
			$sql = "select a.sequence,c.name from content as a, content_video as b, playlist as c where b.playlist=$playlist and b.id = a.id and c.id = $playlist and a.type = 'video'";
			$result = mysql_query($sql) or die(mysql_error());
			$r = "";	
			while($row=mysql_fetch_array($result))
			{
				$r[] = $row; 
			}
			$playlist_item=current($r);
		    $playlist_queue = $playlist_item['sequence'];
			$this->playlist_name =  $playlist_item['name'];
			return $playlist_queue;
	}
	function save_playlist_queue()
	{
		 		process_sql_post($this->post,'playlist','update','id',$this->post['id']);
	}
	function get_songs_for_playlist()
	{
		 	$w[0]='playlist';
			$w[1]=$this->post['id'];
			$w[2]='=';
			$where[]=$w;
			$o['order'][0]='order';
			$o['order'][1]='ASC';
			$data=get_sql('playlist_media','id',$where,$o);
			
			unset($where);
			$w[0]='type';
			$w[1]='song';
			$w[2]='=';
			$where[]=$w;
			
			
			$songs=get_sql('media','id',$where,NULL);
			
			
			unset($where);
			$w[0]='id';
			$w[1]=$this->post['id'];
			$w[2]='=';
			$where[]=$w;
			
			$playlist_data=get_sql('playlist','id',$where,NULL);
			$playlist_item=current($playlist_data);
		
			foreach ($data as $x)
			{
				$song=$songs[$x['media']];
				$song['order']=$x['order'];
				
				$playlist[$x['order']]=$song;
			}
			
			if($playlist_item['shuffle']=='true')
			{
				shuffle($playlist);
			}
			
			return $playlist;
	}
	function add_song_to_playlist()
	{
		
		
		
		//get latest order number
	    $w[0]='playlist';
		$w[1]=$this->post['id'];
		$w[2]='=';
		$where[]=$w;
		$data=get_sql('playlist_media','id',$where,NULL);
		$v=count($data)+1;
		
		$w[0]='media';
		$w[1]=$this->post['songid'];
		$w[2]='=';
		$where[]=$w;
		$d2=get_sql('playlist_media','id',$where,NULL);
		
		if(empty($d2))
		{
			$post['order']=$v;
			$post['playlist']=$this->post['id'];
			$post['media']=$this->post['songid'];
			process_sql_post($post,'playlist_media','add',NULL,NULL);
			printmessage('Successfully Added '.$this->post['songid'].' to Playlist '.$this->post['id']);
		}
		else
		{
			printmessage('Song is already in playlist');
		}
		
		
	}
	
	function remove_song_from_playlist()
	{
		
		$w[0]='playlist';
		$w[1]=$this->post['id'];
		$w[2]='=';
		$where[]=$w;
	
		$w[0]='media';
		$w[1]=$this->post['songid'];
		$w[2]='=';
		$where[]=$w;
	
		$this->data=get_sql('playlist_media','id',$where,NULL);
	
		
	
		//get latest order number
	    
		
		if(!empty($this->data))
		{
				$delete_this=current($this->data);
				delete_sql("playlist_media","id",$delete_this['id'],NULL);
	
			printmessage('Song Deleted');
	
			$this->reorder_playlist();
			
		}
		else
		{
			printmessage('Song is not on the playlist');
		}
		
		
	}
	function reorder_playlist()
	{
		
		$w[0]='playlist';
		$w[1]=$this->post['id'];
		$w[2]='=';
		$where[]=$w;

		
		$data=get_sql('playlist_media','id',$where,NULL);
	
		
		$z=0;
		
		foreach($data as $x)
		{
			$post['order']=$z;
			process_sql_post($post,'playlist_media','update','id',$x['id']);
			$z++;
		}

		
	}
	
	function shuffle_playlist()
	{
		
	}

	
}
?>